package com.ctshk.sapp.system.controller;

import java.util.List;

import javax.validation.Valid;

import com.ctshk.common.enums.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.system.dto.QueryAgentByNameDTO;
import com.ctshk.rpc.system.dto.SysAgentDTO;
import com.ctshk.rpc.system.dto.SysAgentQueryListDTO;
import com.ctshk.rpc.system.req.SysAddAgentReq;
import com.ctshk.rpc.system.req.SysAgentApprovalReq;
import com.ctshk.rpc.system.req.SysAgentQueryListReq;
import com.ctshk.rpc.system.req.SysAgentUpdateStatusReq;
import com.ctshk.rpc.system.service.ISysAgentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 代理商 前端控制器
 * </p>
 *
 * @author 张志朋
 * @since 2021-02-04
 */
@Api(value = "代理商", tags = "代理商")
@RestController
@RequestMapping("/sys/agent")
public class SysAgentController extends BaseController {

    @DubboReference
    private ISysAgentService sysAgentService;

    @ApiOperation(value = "新增代理商(仅保存)", notes = "新增代理商(仅保存)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增代理商(仅保存)", operDesc = "新增代理商(仅保存)")
    @PostMapping("/addForSave")
    public Result addForSave(@RequestBody @Valid SysAddAgentReq sysAddAgentReq) {
        int addType = 1;
        return sysAgentService.add(sysAddAgentReq, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "新增代理商(走审批流程)", notes = "新增代理商(走审批流程)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增代理商(走审批流程)", operDesc = "新增代理商(走审批流程)")
    @PostMapping("/addForApproval")
    public Result addForApproval(@RequestBody @Valid SysAddAgentReq sysAddAgentReq) {
        int addType = 2;
        return sysAgentService.add(sysAddAgentReq, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "根据id查询代理商详情", notes = "根据id查询代理商详情")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "根据id查询代理商详情", operDesc = "根据id查询代理商详情")
    @GetMapping("/info/{id}")
    public Result<SysAgentDTO> query(@PathVariable Long id) {
        return sysAgentService.query(id);
    }

    @ApiOperation(value = "编辑代理商(仅保存)", notes = "编辑代理商(仅保存)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "编辑代理商(仅保存)", operDesc = "编辑代理商(仅保存)")
    @PostMapping("/editForSave")
    public Result editForSave(@RequestBody SysAddAgentReq req) {
        int addType = 1;
        return sysAgentService.editAgent(req, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "编辑代理商(走审批流程)", notes = "编辑代理商(走审批流程)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "编辑代理商(走审批流程)", operDesc = "编辑代理商(走审批流程)")
    @PostMapping("/editForApproval")
    public Result editForApproval(@RequestBody SysAddAgentReq req) {
        int addType = 2;
        return sysAgentService.editAgent(req, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "代理商管理列表查询", notes = "代理商管理列表查询")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "代理商管理列表查询", operDesc = "代理商管理列表查询")
    @PostMapping("/queryAgentList")
    public Result<PageResponse<SysAgentQueryListDTO>> queryAgentList(@RequestBody SysAgentQueryListReq req) {
        return Result.success(sysAgentService.queryAgentList(req, getCurrentUser(getRequest())));
    }

    @ApiOperation(value = "代理商管理列表查询(验证)", notes = "代理商管理列表查询(验证)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "代理商管理列表查询", operDesc = "代理商管理列表查询")
    @PostMapping("/queryAgentListPer")
    public Result<PageResponse<SysAgentQueryListDTO>> queryAgentListPer(@RequestBody SysAgentQueryListReq req) {
        return Result.success(sysAgentService.queryAgentListPer(req, getCurrentUser(getRequest())));
    }

    @ApiOperation(value = "通过名称查询代理商", notes = "通过名称查询代理商")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = true, value = "代理商名称")
    })
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "通过名称查询代理商", operDesc = "通过名称查询代理商")
    @GetMapping("/queryAgentByName")
    public Result<List<QueryAgentByNameDTO>> queryAgentByName(@RequestParam(value = "name", required = true) String name) {
        return sysAgentService.queryAgentByName(name);
    }

    @ApiOperation(value = "代理商审核", notes = "代理商审核")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "代理商审核", operDesc = "代理商审核")
    @PostMapping("/approval")
    public Result approval(@RequestBody @Valid SysAgentApprovalReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return sysAgentService.approval(req);
    }

    @ApiOperation(value = "修改代理商状态", notes = "修改代理商状态")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "修改代理商状态", operDesc = "修改代理商状态")
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody @Valid SysAgentUpdateStatusReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return sysAgentService.updateStatus(req);
    }

    @ApiOperation(value = "删除代理商", notes = "删除代理商")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @GetMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        return sysAgentService.deleteById(id);
    }
}








